﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Entities;
using Xenta.Enums;
using Xenta.Plugins;

namespace Xenta.Tests.Plugins
{
    [TestFixture]
    public class AuthorizeNetIPNHandlerTests : InfrastructureTest
    {
        [TestCase(10,
            "373125",
            "secret",
            "123456",
            "dfa4290759b0c55b6c70f84d62c45ec7",
            "AUTH_CAPTURE",
            "9e879f10-f91d-4749-ae3e-baf757b2def4",
            "x_invoice_num",
            "APPROVED")]
        public void Can_HandleIPN(decimal amount,
            string x_login,
            string txKey,
            string x_trans_id,
            string x_MD5_Hash,
            string x_type, 
            string x_description,
            string x_invoice_num,
            string x_response_code)
        {
            var ipn = new Dictionary<String, String>
            {
                {"x_response_code", x_response_code},
                {"x_type", x_type},
                {"x_invoice_num", x_invoice_num},
                {"x_description", x_description},
                {"x_MD5_Hash", x_MD5_Hash},
                {"x_trans_id", x_trans_id}
            };
            var handler = new AuthorizeNetPaymentProcessor();
            var guid = Guid.Empty;
            var status = PaymentFlags.None;

            string ret = handler.Identify(ipn, out guid);

            Assert.IsNullOrEmpty(ret);
            guid.AreEqual(Guid.Parse(x_description));

            var payment = new PaymentEntity
            {
                Invoice = new InvoiceEntity
                {
                    Order = new OrderEntity
                    {
                        Seller = new SellerEntity
                        {
                            Account = new AccountEntity
                            {
                                Attributes = new List<AccountAttributeEntity>
                                {
                                    new AccountAttributeEntity
                                    {
                                        Key = "Plugin.AuthorizeNetPaymentProcessor.APILoginID",
                                        Value = x_login
                                    },
                                    new AccountAttributeEntity
                                    {
                                        Key = "Plugin.AuthorizeNetPaymentProcessor.TransactionKey",
                                        Value = txKey
                                    }
                                }
                            }
                        }
                    }
                },
                Guid = guid,
                Amount = amount
            };
            ret = handler.Handle(ipn, payment, out status);

            Assert.IsNullOrEmpty(ret);
            status.AreEqual(PaymentFlags.Captured);
        }
    }
}
